package com.hxkcloud.patient.activitys;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.config.ApplicationConfig;
import com.hxkcloud.patient.config.UrlConfig;
import com.hxkcloud.patient.recorder.view.AudioRecorderButton;
import com.hxkcloud.patient.utils.BrightnessUtils;
import com.hxkcloud.patient.utils.ImageOptionUtils;
import com.hxkcloud.patient.utils.ImageUtil;
import com.hxkcloud.patient.utils.ImageVideoUtils;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by ray_lei on 2016/7/20.
 */
public class CertificationPicsActivity extends BaseActivity implements View.OnClickListener {

    //title
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private ImageView iv1;
    private ImageView iv2;
    private ImageView iv3;
    private ImageView iv4;
    private ImageView iv5;
    private ImageView iv6;
    private ImageView iv7;
    private ImageView iv8;
    private ImageView iv9;
    private LinearLayout ll_voice;
    private TextView tv_voice;
    private Button bt_save;

    private PopupWindow popup_imgs;//选择图片、录音
    private Button bt_ptakep;
    private View view_pline1;
    private Button bt_pphoto;
    private View view_pline2;
    private AudioRecorderButton bt_psound;
    private Button bt_pcancel;

    private static final int TAKE_PHOTO_PICTURE = 1;
    private static final int SELECT_PICTURE = 3;

    private static final int DEL_PHOTO = 10;
    private static final int DEL_VOICE = 11;

    private Button submitBtn;

    // 上传文件的内容
    private File cameraPath;
    private File cameraFile;
    private String cameraFileName;

    private ArrayList<String> imageUris;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_certpics);
        initView();
        initData();
        initListener();
    }

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {

        }
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(mContext.getResources().getString(R.string.certification_text_title));

        iv1 = (ImageView) findViewById(R.id.iv_certpic_img1);
        iv2 = (ImageView) findViewById(R.id.iv_certpic_img2);
        iv3 = (ImageView) findViewById(R.id.iv_certpic_img3);
        iv4 = (ImageView) findViewById(R.id.iv_certpic_img4);
        iv5 = (ImageView) findViewById(R.id.iv_certpic_img5);
        iv6 = (ImageView) findViewById(R.id.iv_certpic_img6);
        iv7 = (ImageView) findViewById(R.id.iv_certpic_img7);
        iv8 = (ImageView) findViewById(R.id.iv_certpic_img8);
        iv9 = (ImageView) findViewById(R.id.iv_certpic_img9);


        iv1.setOnClickListener(this);
        iv2.setOnClickListener(this);
        iv3.setOnClickListener(this);
        iv4.setOnClickListener(this);
        iv5.setOnClickListener(this);
        iv6.setOnClickListener(this);
        iv7.setOnClickListener(this);
        iv8.setOnClickListener(this);
        iv9.setOnClickListener(this);

        LinearLayout.LayoutParams lp_iv = new LinearLayout.LayoutParams((screenWidth - ImageUtil.dip2px(30, mContext)) / 3, screenWidth / 3);
        LinearLayout.LayoutParams lp_iv1 = new LinearLayout.LayoutParams((screenWidth - ImageUtil.dip2px(30, mContext)) / 3, screenWidth / 3);
        lp_iv1.leftMargin = ImageUtil.dip2px(5, mContext);
        iv1.setLayoutParams(lp_iv);
        iv2.setLayoutParams(lp_iv1);
        iv3.setLayoutParams(lp_iv1);
        iv4.setLayoutParams(lp_iv);
        iv5.setLayoutParams(lp_iv1);
        iv6.setLayoutParams(lp_iv1);
        iv7.setLayoutParams(lp_iv);
        iv8.setLayoutParams(lp_iv1);
        iv9.setLayoutParams(lp_iv1);

        submitBtn = (Button) findViewById(R.id.certpic_btn_submit);

        initPopup();

    }

    @Override
    protected void initListener() {
        iv_back.setOnClickListener(this);
        submitBtn.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        cameraPath = new File(ApplicationConfig.PicAndVideoUrl);
        if (!cameraPath.exists()) {
            cameraPath.mkdirs();
        }
        if (getIntent().hasExtra("imageUris")) {
            imageUris = getIntent().getStringArrayListExtra("imageUris");
            for (int i = 0; i < imageUris.size(); i++) {
                String imgStr = imageUris.get(i);
                switch (i) {
                    case 0:
                        iv1.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv1, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            imageLoader.displayImage(UrlConfig.image_host + imgStr, iv1, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }

                        break;
                    case 1:
                        iv2.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv2, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            imageLoader.displayImage(UrlConfig.image_host + imgStr, iv2, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                    case 2:
                        iv3.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv3, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            imageLoader.displayImage(UrlConfig.image_host + imgStr, iv3, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                    case 3:
                        iv4.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv4, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageLoader.displayImage(UrlConfig.image_host + imgStr, iv4, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                    case 4:
                        iv5.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv5, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            imageLoader.displayImage(UrlConfig.image_host + imgStr, iv5, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                    case 5:
                        iv6.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv6, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            imageLoader.displayImage(UrlConfig.image_host + imgStr, iv6, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                    case 6:
                        iv7.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv7, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            imageLoader.displayImage(UrlConfig.image_host + imgStr, iv7, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                    case 7:
                        iv8.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv8, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            imageLoader.displayImage(UrlConfig.image_host + imgStr, iv8, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                    case 8:
                        iv9.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv9, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            imageLoader.displayImage(UrlConfig.image_host + imgStr, iv9, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                }
            }
            if (getIndexIv(imageUris.size()) != null) {
                getIndexIv(imageUris.size()).setVisibility(View.VISIBLE);
            }
        } else {
            imageUris = new ArrayList<>();
        }

    }

    private void initPopup() {
        popup_imgs = new PopupWindow(this);
        View viewImgs = LayoutInflater.from(mContext).inflate(R.layout.popup_addreport, null);
        popup_imgs.setContentView(viewImgs);

        popup_imgs.setWidth(screenWidth);
        popup_imgs.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popup_imgs.setFocusable(true);
        popup_imgs.setTouchable(true);
        popup_imgs.setOutsideTouchable(true);
        popup_imgs.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popup_imgs.setAnimationStyle(R.style.popupwindow_anim_default);

        bt_ptakep = (Button) viewImgs.findViewById(R.id.bt_addreport_takep);
        view_pline1 = viewImgs.findViewById(R.id.view_addreport_line1);
        bt_pphoto = (Button) viewImgs.findViewById(R.id.bt_addreport_photo);
        view_pline2 = viewImgs.findViewById(R.id.view_addreport_line2);
        view_pline2.setVisibility(View.GONE);
        bt_psound = (AudioRecorderButton) viewImgs.findViewById(R.id.bt_addreport_sound);
        bt_psound.setVisibility(View.GONE);
        bt_pcancel = (Button) viewImgs.findViewById(R.id.bt_addreport_cancel);

        bt_ptakep.setOnClickListener(this);
        bt_pphoto.setOnClickListener(this);
        bt_pcancel.setOnClickListener(this);


        popup_imgs.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(CertificationPicsActivity.this);
            }
        });
    }

    //0全部 1图片 2录音
    private void showPopup(int type) {
        closeInputMethodManager();
//        bt_psound.setText(R.string.soundrecord);
//        if (type == 0) {
//            bt_ptakep.setVisibility(View.VISIBLE);
//            view_pline1.setVisibility(View.VISIBLE);
//            bt_pphoto.setVisibility(View.VISIBLE);
//            view_pline2.setVisibility(View.VISIBLE);
//            bt_psound.setVisibility(View.VISIBLE);
//        } else if (type == 1) {
//            bt_ptakep.setVisibility(View.VISIBLE);
//            view_pline1.setVisibility(View.VISIBLE);
//            bt_pphoto.setVisibility(View.VISIBLE);
//            view_pline2.setVisibility(View.GONE);
//            bt_psound.setVisibility(View.GONE);
//        } else if (type == 2) {
//            bt_ptakep.setVisibility(View.GONE);
//            view_pline1.setVisibility(View.GONE);
//            bt_pphoto.setVisibility(View.GONE);
//            view_pline2.setVisibility(View.GONE);
//            bt_psound.setVisibility(View.VISIBLE);
//        }
        popup_imgs.showAtLocation(ll_title, Gravity.BOTTOM, 0, 0);
        BrightnessUtils.lightOff(CertificationPicsActivity.this);
    }

    private void seePhotos(int index) {
        Intent iPhotos = new Intent(mContext, PhotosActivity.class);
        iPhotos.putExtra("index", index);
        iPhotos.putStringArrayListExtra("urls", imageUris);
        startActivityForResult(iPhotos, DEL_PHOTO);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.certpic_btn_submit:
                Intent intent = new Intent();
                intent.putStringArrayListExtra("pics", imageUris);
                setResult(3, intent);
                finish();
                break;
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.bt_addreport_takep:
                popup_imgs.dismiss();
                Intent iTakep = new Intent();
                iTakep.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                cameraFileName = System.currentTimeMillis() + ".jpg";
                cameraFile = new File(cameraPath, cameraFileName);
                if (cameraFile.exists()) {
                    cameraFile.delete();
                }
                iTakep.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(cameraFile));
                startActivityForResult(iTakep,
                        TAKE_PHOTO_PICTURE);
                break;
            case R.id.bt_addreport_photo:
                popup_imgs.dismiss();
                Intent iPhoto = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                iPhoto.setDataAndType(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(iPhoto,
                        SELECT_PICTURE);
                break;
            case R.id.bt_addreport_cancel:
                popup_imgs.dismiss();
                break;
            case R.id.iv_certpic_img1:
                if (imageUris.size() == 0) {
                    showPopup(0);
                } else {
                    seePhotos(0);
                }
                break;
            case R.id.iv_certpic_img2:
                if (imageUris.size() == 1) {
                    showPopup(1);
                } else {
                    seePhotos(1);
                }
                break;
            case R.id.iv_certpic_img3:
                if (imageUris.size() == 2) {
                    showPopup(1);
                } else {
                    seePhotos(2);
                }
                break;
            case R.id.iv_certpic_img4:
                if (imageUris.size() == 3) {
                    showPopup(1);
                } else {
                    seePhotos(3);
                }
                break;
            case R.id.iv_certpic_img5:
                if (imageUris.size() == 4) {
                    showPopup(1);
                } else {
                    seePhotos(4);
                }
                break;
            case R.id.iv_certpic_img6:
                if (imageUris.size() == 5) {
                    showPopup(1);
                } else {
                    seePhotos(5);
                }
                break;
            case R.id.iv_certpic_img7:
                if (imageUris.size() == 6) {
                    showPopup(1);
                } else {
                    seePhotos(6);
                }
                break;
            case R.id.iv_certpic_img8:
                if (imageUris.size() == 7) {
                    showPopup(1);
                } else {
                    seePhotos(7);
                }
                break;
            case R.id.iv_certpic_img9:
                if (imageUris.size() == 8) {
                    showPopup(1);
                } else {
                    seePhotos(8);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case TAKE_PHOTO_PICTURE:
                    if (cameraFile != null) {
//                        ImageVideoUtils.compressPicture(cameraFile, cameraFile);
//                        Bitmap mBitmap = BitmapFactory.decodeFile(cameraFile.getAbsolutePath());
//                        getCurIv().setImageBitmap(mBitmap);

                        String u = cameraFile.getAbsolutePath();
                        imageLoader.displayImage("file://" + u, getCurIv(), ImageOptionUtils.getInstance().getSimplePicOptions());

                        if (getNextIv() != null) {
                            getNextIv().setVisibility(View.VISIBLE);
                        }

                        imageUris.add(cameraFile.getAbsolutePath());
                        submitBtn.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        submitBtn.setEnabled(true);
                    }
                    break;
                case SELECT_PICTURE:
                    if (data.getData() != null) {
                        Uri uri = data.getData();
                        if (uri != null) {
                            String u = ImageVideoUtils.getImagePathForUri(uri, mContext);
//                            cameraFile = new File(u);
//                            ImageVideoUtils.compressPicture(cameraFile, cameraFile);
//                            Bitmap mBitmap = BitmapFactory.decodeFile(cameraFile.getAbsolutePath());
//                            getCurIv().setImageBitmap(mBitmap);
                            imageLoader.displayImage("file://" + u, getCurIv(), ImageOptionUtils.getInstance().getSimplePicOptions());
                            if (getNextIv() != null) {
                                getNextIv().setVisibility(View.VISIBLE);
                            }
                            imageUris.add(u);
                            submitBtn.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                            submitBtn.setEnabled(true);
                        }
                    }
                    break;
                case DEL_PHOTO:
                    if (data.hasExtra("index")) {
                        int del = data.getIntExtra("index", 0);
                        if (del < imageUris.size()) {
                            if (del == imageUris.size() - 1) {
                                getLastIv().setImageResource(R.drawable.report_add);
                                if (getCurIv() != null) {
                                    getCurIv().setVisibility(View.GONE);
                                }
                            } else {
                                for (int i = del; i < imageUris.size() - 1; i++) {

                                    String str = imageUris.get(i + 1);
                                    if (imageUris.get(i).startsWith("http")) {
                                        imageLoader.displayImage(str, getIndexIv(i), ImageOptionUtils.getInstance().getSimplePicOptions());
                                    } else {
                                        cameraFile = new File(imageUris.get(i + 1));
                                        Bitmap mBitmap = BitmapFactory.decodeFile(cameraFile.getAbsolutePath());
                                        getIndexIv(i).setImageBitmap(mBitmap);
                                    }

                                }
                                getLastIv().setImageResource(R.drawable.report_add);
                                if (getCurIv() != null) {
                                    getCurIv().setVisibility(View.GONE);
                                }
                            }
                            imageUris.remove(del);
                            submitBtn.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                            submitBtn.setEnabled(true);
                        }
                    }
                    break;
            }
        }
    }

    private ImageView getIndexIv(int index) {
        ImageView iv = null;
        switch (index) {
            case 0:
                iv = iv1;
                break;
            case 1:
                iv = iv2;
                break;
            case 2:
                iv = iv3;
                break;
            case 3:
                iv = iv4;
                break;
            case 4:
                iv = iv5;
                break;
            case 5:
                iv = iv6;
                break;
            case 6:
                iv = iv7;
                break;
            case 7:
                iv = iv8;
                break;
            case 8:
                iv = iv9;
                break;
        }
        return iv;
    }

    private ImageView getCurIv() {
        ImageView iv = null;
        if (imageUris != null) {
            switch (imageUris.size()) {
                case 0:
                    iv = iv1;
                    break;
                case 1:
                    iv = iv2;
                    break;
                case 2:
                    iv = iv3;
                    break;
                case 3:
                    iv = iv4;
                    break;
                case 4:
                    iv = iv5;
                    break;
                case 5:
                    iv = iv6;
                    break;
                case 6:
                    iv = iv7;
                    break;
                case 7:
                    iv = iv8;
                    break;
                case 8:
                    iv = iv9;
                    break;
                default:
                    iv = null;
                    break;
            }
        } else {
            iv = null;
        }
        return iv;
    }

    private ImageView getLastIv() {
        ImageView iv = null;
        if (imageUris != null) {
            switch (imageUris.size()) {
                case 0:

                    break;
                case 1:
                    iv = iv1;
                    break;
                case 2:
                    iv = iv2;
                    break;
                case 3:
                    iv = iv3;
                    break;
                case 4:
                    iv = iv4;
                    break;
                case 5:
                    iv = iv5;
                    break;
                case 6:
                    iv = iv6;
                    break;
                case 7:
                    iv = iv7;
                    break;
                case 8:
                    iv = iv8;
                    break;
                default:
                    iv = iv9;
                    break;
            }
        } else {
            iv = null;
        }
        return iv;
    }

    private ImageView getNextIv() {
        ImageView iv = null;
        if (imageUris != null) {
            switch (imageUris.size()) {
                case 0:
                    iv = iv2;
                    break;
                case 1:
                    iv = iv3;
                    break;
                case 2:
                    iv = iv4;
                    break;
                case 3:
                    iv = iv5;
                    break;
                case 4:
                    iv = iv6;
                    break;
                case 5:
                    iv = iv7;
                    break;
                case 6:
                    iv = iv8;
                    break;
                case 7:
                    iv = iv9;
                    break;
                case 8:
                    iv = null;
                    break;
                default:
                    iv = null;
                    break;
            }
        } else {
            iv = null;
        }
        return iv;
    }
}
